unit Unit1; 

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  ExtCtrls, Buttons, Menus, ComCtrls, SynHighlighterAny, SynMemo;

type

  { TForm1 }

  TForm1 = class(TForm)
    MainMenu1: TMainMenu;
    menuFile: TMenuItem;
    MenuItem1: TMenuItem;
    menuCompile: TMenuItem;
    menuSave: TMenuItem;
    menuRunApp: TMenuItem;
    MenuItem3: TMenuItem;
    menuAbout: TMenuItem;
    menuOptions: TMenuItem;
    Options: TMenuItem;
    menuQuit: TMenuItem;
    menuNew: TMenuItem;
    menuSaveAs: TMenuItem;
    menuLoad: TMenuItem;
    OpenDialog1: TOpenDialog;
    Panel1: TPanel;
    SaveDialog1: TSaveDialog;
    speedOpen: TSpeedButton;
    speedSave: TSpeedButton;
    speedNew: TSpeedButton;
    speedExit: TSpeedButton;
    speedCompile: TSpeedButton;
    speedRunApp: TSpeedButton;
    speedOptions: TSpeedButton;
    StatusBar1: TStatusBar;
    SynBaCon: TSynAnySyn;
    SynMemo1: TSynMemo;

    procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);
    procedure FormCreate(Sender: TObject);
    procedure menuAboutClick(Sender: TObject);
    procedure menuCompileClick(Sender: TObject);
    procedure menuRunAppClick(Sender: TObject);
    procedure menuLoadClick(Sender: TObject);
    procedure menuNewClick(Sender: TObject);
    procedure menuOptionsClick(Sender: TObject);
    procedure menuQuitClick(Sender: TObject);
    procedure menuSaveAsClick(Sender: TObject);
    procedure menuSaveClick(Sender: TObject);
    procedure speedRunAppClick(Sender: TObject);
    procedure speedOpenClick(Sender: TObject);
    procedure speedSaveClick(Sender: TObject);
    procedure speedNewClick(Sender: TObject);
    procedure speedExitClick(Sender: TObject);
    procedure speedCompileClick(Sender: TObject);
    procedure speedOptionsClick(Sender: TObject);
    procedure SynMemo1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
      );
    procedure SynMemo1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    procedure LoadFile;
    procedure SaveFileAs;
    procedure SaveFile;
    procedure Exit;
    procedure NewFile;
    procedure StatusBarUpdate;
    procedure Start;
    procedure ExApp;
    { private declarations }
  public
    { public declarations }
  end; 

var
  Form1: TForm1; 
  compilername: string;
  dateiname: string;
  compiled: boolean;
  exename: string;
  directory: string;
  pathtoscript: string;
implementation
  uses Unit2, Unit3;
{ TForm1 }

resourcestring
SWarning = 'Warning!';
SDataModified = 'The file has been changed.'+SLinebreak + 'What do you want to do?';
SDiscard = 'Discard changes';
SKeep = 'Keep changes';
SRunscript ='runscript_tinyide.sh';

procedure TForm1.menuLoadClick(Sender: TObject);
begin
LoadFile;
end;

//Check if file has changed
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: boolean);
begin
  CanClose := not SynMemo1.Modified;
           if not CanClose then
              case QuestionDlg (SWarning, SDataModified, mtWarning, [mrYes, sKeep, mrNo, SDiscard, mrCancel],0) of
                 mrNo: CanClose := True;
                 mrYes: begin
                      CanClose := True;
                      SaveFile;
                        end;
              end;
end;

//Initial settings
procedure TForm1.FormCreate(Sender: TObject);
begin
  compiled :=False;
  compilername:='';
  StatusBarUpdate;
  directory:=ExtractFilePath( Application.ExeName );
  pathtoscript:=ExtractFilePath( Application.ExeName );
  pathtoscript := pathtoscript +SRunscript;
end;

procedure TForm1.menuAboutClick(Sender: TObject);
begin
  Form3.Showmodal;
end;

procedure TForm1.menuCompileClick(Sender: TObject);
begin
  Start;
end;

procedure TForm1.menuRunAppClick(Sender: TObject);
begin
  ExApp;
end;

procedure TForm1.menuNewClick(Sender: TObject);
begin
  NewFile;
end;

procedure TForm1.menuOptionsClick(Sender: TObject);
begin
  Form2.ShowModal;
end;

procedure TForm1.menuQuitClick(Sender: TObject);
begin
  Exit;
end;

procedure TForm1.menuSaveAsClick(Sender: TObject);
begin
SaveFileAs;
end;

procedure TForm1.menuSaveClick(Sender: TObject);
begin
  SaveFile;
end;

procedure TForm1.speedRunAppClick(Sender: TObject);
begin
  ExApp;
end;

procedure TForm1.speedOpenClick(Sender: TObject);
begin
LoadFile;
end;

procedure TForm1.speedSaveClick(Sender: TObject);
begin
  SaveFile;
end;

procedure TForm1.speedNewClick(Sender: TObject);
begin
  NewFile;
end;

procedure TForm1.speedExitClick(Sender: TObject);
begin
   Exit;
end;

procedure TForm1.speedCompileClick(Sender: TObject);
begin
  Start;
end;

procedure TForm1.speedOptionsClick(Sender: TObject);
begin
  Form2.ShowModal;
end;

procedure TForm1.SynMemo1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  StatusBarUpdate;

end;

procedure TForm1.SynMemo1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  StatusBarUpdate;
end;

//Load file and check if it exists
procedure TForm1.LoadFile;
begin
  if OpenDialog1.Execute then
   begin
     dateiname :=OpenDialog1.FileName;
     SynMemo1.Lines.LoadFromFile(OpenDialog1.FileName);
     StatusBarUpdate;
     directory := ExtractFilePath(dateiname);
     chdir(directory);
     exename :=Leftstr(dateiname,(Length(dateiname) - 4));
     if fileexists(exename) then
      begin
           compiled := True;
           speedRunApp.Enabled:=True;
           menuRunApp.Enabled:=True;
      end;
     if (compilername<>'') and (dateiname <>'') then
      begin
           speedCompile.Enabled:=True;
           menuCompile.Enabled:=True;
      end;
   end;
end;

//Save file as ... Procedure will be renamed soon.
procedure TForm1.SaveFileAs;
begin
    SaveDialog1.Filename:=dateiname;
       if SaveDialog1.Execute then
          begin
          dateiname :=SaveDialog1.FileName;
          if fileexists(SaveDialog1.FileName) then
             begin
             if MessageDlg ('File already exists.'+#13+'Do you want to replace?',mtWarning,[mbYes,mbNo],0)=mrNo then
              begin
                  SaveDialog1.Close;
              end;
             end;
             SynMemo1.Lines.SaveToFile(dateiname);
             directory := ExtractFilePath(dateiname);
             chdir(directory);
             StatusBarUpdate;
             SynMemo1.Modified:=False;
             speedRunApp.Enabled:=False;
             menuRunApp.Enabled:=False;
             if (compilername<>'') and (dateiname <>'') then
                begin
                     speedCompile.Enabled:=True;
                     menuCompile.Enabled:=True;
                end;
          end;
end;

procedure TForm1.SaveFile;
begin
 if dateiname <>'' then
    begin
        SynMemo1.Lines.SaveToFile(dateiname);
        SynMemo1.Modified:=False;
    end
    else
     SaveFileAs;
 end;

procedure TForm1.Exit;
begin
   Close;
end;

//Update the status bar
procedure TForm1.StatusBarUpdate;
begin
   StatusBar1.SimpleText:='Line: '+ IntToStr(SynMemo1.CaretY) + ' Column: '+IntToStr(SynMemo1.Caretx) + ' File: '+dateiname;
end;

//Clear the SynMemo if unmodified and restore initial settings
procedure TForm1.NewFile;
begin
   if SynMemo1.Modified then
    begin
      case QuestionDlg (SWarning, SDataModified, mtWarning, [mrYes, sKeep, mrNo, SDiscard, mrCancel],0) of
     mrNo: begin
                SynMemo1.Modified :=False;
           end;
      end;
    end;
    if not SynMemo1.Modified then
       begin
            SynMemo1.Clear;
            compiled:=False;
            dateiname:='';
            speedRunApp.Enabled:=False;
            menuRunApp.Enabled:=False;
            speedCompile.Enabled:=False;
            menuCompile.Enabled:=False;
       end;
    StatusBarUpdate;
end;

procedure TForm1.Start;
begin
 try
    if (Sysutils.ExecuteProcess(compilername, dateiname))=0 then
       begin
            showmessage('Compilation completed! Now you can execute your program');
            compiled :=True;
            exename :=Leftstr(dateiname,(Length(dateiname) - 4));
            speedRunApp.Enabled:=True;
            menuRunApp.Enabled:=True;
       end;

  except
        ShowMessage ('Could not compile. Did you forget' +#13+'to enter options and filename?');
   end;
end;

procedure TForm1.ExApp;
begin
    if fileexists(exename) then
    Sysutils.ExecuteProcess('/usr/bin/xterm',['-e', pathtoscript, exename]);
end;


initialization
  {$I Unit1.lrs}

end.

